package com.atguigu.guli.service.statistics.service.impl;

import com.atguigu.guli.service.base.exception.GuliException;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.statistics.entity.Daily;
import com.atguigu.guli.service.statistics.mapper.DailyMapper;
import com.atguigu.guli.service.statistics.service.DailyService;
import com.atguigu.guli.service.statistics.service.UcenterFeignClient;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-08-16
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    UcenterFeignClient ucenterFeignClient;
    //1、生成统计数据
    //day的格式必须为yyyy--MM-dd
    @Override
    public void genDaily(String day) {
        Daily daily= new Daily();
        daily.setDateCalculated(day);//统计日期
        daily.setCourseNum(RandomUtils.nextInt(5,100));//新增课程数量
        daily.setVideoViewNum(RandomUtils.nextInt(3000,10000));//视频点播次数
        daily.setLoginNum(RandomUtils.nextInt(100,5000));//每日登陆用户的数量

        R registNumR = ucenterFeignClient.genRegistNum(day);
        if (!registNumR.getSuccess()){
            throw new GuliException(ResultCodeEnum.GENERATE_STATISTICS_ERROR);
        }
        int registNum = Integer.parseInt(registNumR.getData().get("num").toString());

        daily.setRegisterNum(registNum);//每日新增用户数量
        //查询day对应的统计数据存到表中
        baseMapper.insert(daily);
        //去ucenter里Admin里开始远程访问
        //在UcenterFeignClient接口中创建@FeignClient(value = "service-ucenter")开启
    }

    @Override
    public Map<String, Object> getStatistics(String begin, String end) {

        LambdaQueryWrapper<Daily> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.ge(Daily::getDateCalculated,begin);
        queryWrapper.le(Daily::getDateCalculated,end);
        queryWrapper.orderByAsc(Daily::getDateCalculated);

        List<Daily> dailies = baseMapper.selectList(queryWrapper);
        List dates = new ArrayList();
        List loginNumList = new ArrayList();
        List registNumList = new ArrayList();
        List videoViewNumList = new ArrayList();
        List courseNumList = new ArrayList();
        Map map = new HashMap<>();
        dailies.forEach(daily -> {
            dates.add(daily.getDateCalculated());
            loginNumList.add(daily.getLoginNum());
            registNumList.add(daily.getRegisterNum());
            videoViewNumList.add(daily.getVideoViewNum());
            courseNumList.add(daily.getCourseNum());
        });
        map.put("dates",dates);
        map.put("loginNumList",loginNumList);
        map.put("registNumList",registNumList);
        map.put("videoViewNumList",videoViewNumList);
        map.put("courseNumList",courseNumList);
        return map;
    }
}
